package team.seekers.hula.island.postComment.service;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import team.seekers.hula.common.utils.BaseConvertUtils;
import team.seekers.hula.island.postComment.dao.IslandPostCommentReplyDao;
import team.seekers.hula.island.postComment.entity.dao.IslandPostCommentReplyDO;
import team.seekers.hula.island.postComment.entity.vo.PostCommentReplyListVO;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.user.user.service.UserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author HBC
 */
@Service
public class IslandPostCommentReplyService extends IslandPostCommentReplyDao{

    @Resource
    private IslandPostCommentReplyDao islandPostCommentReplyDao;

    @Resource
    private UserService userService;

    public List<PostCommentReplyListVO> listReply(String commentId) {
        List<IslandPostCommentReplyDO> list = lambdaQuery()
                .eq(IslandPostCommentReplyDO::getPostCommentId, commentId)
                .eq(IslandPostCommentReplyDO::getVisibility, 2)
                .orderByAsc(IslandPostCommentReplyDO::getFloor)
                .list();

        List<PostCommentReplyListVO> postCommentReplyListVOS = IslandPostCommentReplyDO.toListVOList(list);

        for (PostCommentReplyListVO postCommentReplyListVO : postCommentReplyListVOS) {
            postCommentReplyListVO.setUserInfo(userService.getBasicInfoVO(postCommentReplyListVO.getUserId()));
            postCommentReplyListVO.setIsMine(MindUtils.getUserId().equals(postCommentReplyListVO.getUserId()));

            postCommentReplyListVO.setIsLike(postCommentReplyListVO.getLikeUserIds().contains(MindUtils.getUserId()));
            postCommentReplyListVO.setLikeCount(postCommentReplyListVO.getLikeUserIds().size());
        }

        return postCommentReplyListVOS;
    }

    public List<PostCommentReplyListVO> listThree(String commentId) {
        List<IslandPostCommentReplyDO> list = lambdaQuery()
                .eq(IslandPostCommentReplyDO::getPostCommentId, commentId)
                .orderByAsc(IslandPostCommentReplyDO::getFloor)
                .last("limit 3").list();
        return IslandPostCommentReplyDO.toListVOList(list);
    }

    public PostCommentReplyListVO getListVO(String id) {
        IslandPostCommentReplyDO replyDO = getById(id);
        PostCommentReplyListVO postCommentReplyListVO = new PostCommentReplyListVO();
        BeanUtils.copyProperties(replyDO,postCommentReplyListVO);

        postCommentReplyListVO.setUserInfo(userService.getBasicInfoVO(postCommentReplyListVO.getUserId()));
        postCommentReplyListVO.setIsMine(MindUtils.getUserId().equals(postCommentReplyListVO.getUserId()));

        List<String> userIds = BaseConvertUtils.stringToList(replyDO.getLikeUserIds());

        postCommentReplyListVO.setIsLike(userIds.contains(MindUtils.getUserId()));
        postCommentReplyListVO.setLikeCount(userIds.size());

        return postCommentReplyListVO;
    }

    public void likeOrCancel(String replyId, String userId) {
        IslandPostCommentReplyDO commentReplyDO = lambdaQuery().select(IslandPostCommentReplyDO::getId,
                IslandPostCommentReplyDO::getLikeUserIds).eq(IslandPostCommentReplyDO::getId, replyId).one();
        List<String> userIds = BaseConvertUtils.stringToList(commentReplyDO.getLikeUserIds());
        if(userIds.contains(userId)){
            userIds.remove(userId);
        }else{
            userIds.add(userId);
        }
        commentReplyDO.setLikeUserIds(BaseConvertUtils.listToString(userIds));
        updateById(commentReplyDO);
    }

    public void removeByCommentId(String commentId) {
        lambdaUpdate().eq(IslandPostCommentReplyDO::getPostCommentId,commentId).remove();
    }
}
